<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item
            label="默认显示条数"
            prop="size"
            :rules="[
              { required: true, message: '默认显示条数', trigger: 'blur' }
            ]"
          >
            <el-input-number v-model="form.size" :min="1" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="save('form')"
      >确认</el-button>
    </div>
  </div>
</template>
<script>
import { updateObject } from '@/api/portal/manager/columnTemp';
export default {
  name: 'PcNoticeTempV2',
  props: {
    data: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      status: [
        { id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }
      ],
      orders: [
        { id: 0, value: 'DESC', label: '升序' },
        { id: 1, value: 'ASC', label: '降序' }
      ],
      form: {
        isOpenMore: true, // 是否显示更多
        size: 5 // 每页显示几条
      }
    };
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (
            undefined !== newName.pzx &&
            newName.pzx !== '' &&
            newName.pzx !== null
          ) {
            this.form = JSON.parse(newName.pzx);
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    save() {
      this.loading = true;
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form);
          updateObject(this.data)
            .then(res => {
              this.close();
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          this.loading = false;
        }
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    }
  }
};
</script>
<style scoped>
.setting-body {
  width: 100%;
}

.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}

.button-add {
  width: 50px;
  height: 40px;
  float: right;
  text-align: center;
  line-height: 40px;
  margin-right: 20px;
}
.custom-button {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: right;
}
</style>
